package tr.gov.tubitak.bilgem.esya.certviewer.viwerasn;

import sun.security.util.ObjectIdentifier;
import sun.security.x509.ExtendedKeyUsageExtension;
import sun.security.x509.OIDMap;
import tr.gov.tubitak.bilgem.esya.certviewer.Bundle;
import tr.gov.tubitak.bilgem.esya.certviewer.EStringUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: ramazan.girgin
 * Date: 08.12.2011
 * Time: 10:30
 * To change this template use File | Settings | File Templates.
 */
public class EExtendedKeyUsageExtension implements ECertExtension{
    // OID defined in RFC 3280 Sections 4.2.1.13
    // more from http://www.alvestrand.no/objectid/1.3.6.1.5.5.7.3.html
    private static final Map <ObjectIdentifier, String> bundleMap =
            new HashMap <ObjectIdentifier, String> ();

    private static final int[] anyExtendedKeyUsageOidData = {2, 5, 29, 37, 0};
    private static final int[] serverAuthOidData = {1, 3, 6, 1, 5, 5, 7, 3, 1};
    private static final int[] clientAuthOidData = {1, 3, 6, 1, 5, 5, 7, 3, 2};
    private static final int[] codeSigningOidData = {1, 3, 6, 1, 5, 5, 7, 3, 3};
    private static final int[] emailProtectionOidData = {1, 3, 6, 1, 5, 5, 7, 3, 4};
    private static final int[] ipsecEndSystemOidData = {1, 3, 6, 1, 5, 5, 7, 3, 5};
    private static final int[] ipsecTunnelOidData = {1, 3, 6, 1, 5, 5, 7, 3, 6};
    private static final int[] ipsecUserOidData = {1, 3, 6, 1, 5, 5, 7, 3, 7};
    private static final int[] timeStampingOidData = {1, 3, 6, 1, 5, 5, 7, 3, 8};
    private static final int[] OCSPSigningOidData = {1, 3, 6, 1, 5, 5, 7, 3, 9};

    static {
        bundleMap.put(ObjectIdentifier.newInternal(anyExtendedKeyUsageOidData), Bundle.getString("EExtendedKeyUsageExtension.anyExtendedKeyUsage"));
        bundleMap.put(ObjectIdentifier.newInternal(serverAuthOidData), Bundle.getString("EExtendedKeyUsageExtension.serverAuth"));
        bundleMap.put(ObjectIdentifier.newInternal(clientAuthOidData), Bundle.getString("EExtendedKeyUsageExtension.clientAuth"));
        bundleMap.put(ObjectIdentifier.newInternal(codeSigningOidData), Bundle.getString("EExtendedKeyUsageExtension.codeSigning"));
        bundleMap.put(ObjectIdentifier.newInternal(emailProtectionOidData), Bundle.getString("EExtendedKeyUsageExtension.emailProtection"));
        bundleMap.put(ObjectIdentifier.newInternal(ipsecEndSystemOidData), Bundle.getString("EExtendedKeyUsageExtension.ipsecEndSystem"));
        bundleMap.put(ObjectIdentifier.newInternal(ipsecTunnelOidData), Bundle.getString("EExtendedKeyUsageExtension.ipsecTunnel"));
        bundleMap.put(ObjectIdentifier.newInternal(ipsecUserOidData), Bundle.getString("EExtendedKeyUsageExtension.ipsecUser"));
        bundleMap.put(ObjectIdentifier.newInternal(timeStampingOidData), Bundle.getString("EExtendedKeyUsageExtension.timeStamping"));
        bundleMap.put(ObjectIdentifier.newInternal(OCSPSigningOidData), Bundle.getString("EExtendedKeyUsageExtension.OCSPSigning"));
    };
    ExtendedKeyUsageExtension extendedKeyUsageExtension;
    @Override
    public String getName() {
        return Bundle.getString("EExtendedKeyUsageExtension.Name");
    }

    @Override
    public String getShortTitle() {
        List<String> extendedKeyUsage = extendedKeyUsageExtension.getExtendedKeyUsage();
        List<String> strList = new ArrayList<String>();
        for (String oid: extendedKeyUsage) {
            String oidName = Bundle.getString("General.Unknown");
            try {
                ObjectIdentifier objectIdentifier = new ObjectIdentifier(oid);
                if(bundleMap.containsKey(objectIdentifier))
                {
                    oidName = bundleMap.get(objectIdentifier);
                }
            } catch (IOException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
            strList.add(oidName+"("+oid+")");
        }
        return EStringUtils.join(strList.iterator(),"\n");
    }

    @Override
    public String getLongTitle() {
        return getShortTitle();
    }

    @Override
    public boolean isCritical() {
        return extendedKeyUsageExtension.isCritical();
    }

    @Override
    public void encode(byte[] data) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void setAsnObject(Object asnObject) {
        if(asnObject instanceof ExtendedKeyUsageExtension)
        {
            extendedKeyUsageExtension = (ExtendedKeyUsageExtension) asnObject;
        }
        //To change body of implemented methods use File | Settings | File Templates.
    }
}
